package com.ruoyi.web.controller.system;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.Diagnosed;
import com.ruoyi.system.service.IDiagnosedService;
import com.ruoyi.system.service.impl.DiseaseServiceImpl;
import com.ruoyi.web.controller.tool.PageUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 病情诊断Controller
 *
 * @author ruoyi
 * @date 2023-07-04
 */
@Api(tags = "病情诊断模块")
@RestController
@RequestMapping("/system/Diagnosed")
public class DiagnosedController extends BaseController {
    @Autowired
    private IDiagnosedService diagnosedService;

    private class Wrap implements Serializable {
        Diagnosed Diagnosed;
        String DiseaseId;

        @JsonProperty("Diagnosed")
        public com.ruoyi.system.domain.Diagnosed getDiagnosed() {
            return Diagnosed;
        }

        @JsonProperty("DiseaseId")
        public String getDiseaseId() {
            return DiseaseId;
        }
    }

    @Autowired
    DiseaseServiceImpl diseaseService;

    private Wrap convert(Diagnosed origin) {
        Wrap tempWrap = new Wrap();
        tempWrap.DiseaseId = diseaseService.getById(origin.getDiseaseId()).getDiseaseName();
        tempWrap.Diagnosed = origin;
        return tempWrap;
    }

    /**
     * 查询病情诊断列表
     */
    @ApiOperation(value = "查询病情诊断列表")
    @PreAuthorize("@ss.hasPermi('system:Diagnosed:list')")
    @GetMapping("/list")
    public Page<Wrap> list(Diagnosed entity, Page<Diagnosed> page) {

        LambdaQueryWrapper<Diagnosed> lambdaQueryWrapper = new LambdaQueryWrapper<Diagnosed>();
        Page<Diagnosed> old = diagnosedService.page(page, lambdaQueryWrapper);
        Page<Wrap> pageWrap = new Page<>();
        List<Wrap> wraps = new ArrayList<>();
        List<Diagnosed> object = old.getRecords();
        for (Diagnosed item : object) {
            wraps.add(convert(item));
        }
        pageWrap.setRecords(wraps);
        PageUtil pageUtil = new PageUtil();
        pageUtil.setPage(pageWrap, old);
        return pageWrap;
    }

    /**
     * 导出病情诊断列表
     */
    @ApiOperation(value = "导出病情诊断列表")
    @PreAuthorize("@ss.hasPermi('system:Diagnosed:export')")
    @Log(title = "病情诊断", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Diagnosed diagnosed) {
        LambdaQueryWrapper<Diagnosed> queryWrapper = new LambdaQueryWrapper<>();
        List<Diagnosed> list = diagnosedService.list(queryWrapper);
        ExcelUtil<Diagnosed> util = new ExcelUtil<Diagnosed>(Diagnosed.class);
        util.exportExcel(response, list, "病情诊断数据");
    }


    /**
     * 获取病情诊断详细信息
     */
    @ApiOperation(value = "获取病情诊断详细信息")
    @PreAuthorize("@ss.hasPermi('system:Diagnosed:query')")
    @GetMapping(value = "/{Id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(diagnosedService.getById(id));
    }

    /**
     * 新增病情诊断
     */
    @ApiOperation(value = "新增病情诊断")
    @PreAuthorize("@ss.hasPermi('system:Diagnosed:add')")
    @Log(title = "病情诊断", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Diagnosed diagnosed) {
        return toAjax(diagnosedService.save(diagnosed));
    }

    /**
     * 修改病情诊断
     */
    @ApiOperation(value = "修改病情诊断")
    @PreAuthorize("@ss.hasPermi('system:Diagnosed:edit')")
    @Log(title = "病情诊断", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Diagnosed diagnosed) {
        return toAjax(diagnosedService.updateById(diagnosed));
    }


    /**
     * 删除病情诊断
     */
    @ApiOperation(value = "删除病情诊断")
    @PreAuthorize("@ss.hasPermi('system:Diagnosed:remove')")
    @Log(title = "病情诊断", businessType = BusinessType.DELETE)
    @DeleteMapping("/{Ids}")
    public AjaxResult remove(@PathVariable Long[] Ids) {
        return toAjax(diagnosedService.removeBatchByIds(Arrays.asList(Ids)));
    }
}
